pipeline {
    // version:1.0.0

    // 构建参数
    parameters {
        string(name: 'project', defaultValue: '', description: '项目名称')
        string(name: 'sshUrl', defaultValue: '', description: '代码仓库地址')
        string(name: 'branch', defaultValue: 'master', description: 'branch or tag')
        string(name: 'mavenHome', defaultValue: '/data/www/install/apache-maven-3.5.4', description: 'maven路径')

        string(name: 'isClean', defaultValue: 'false', description: '是否执行clean install命令')
        string(name: 'build', defaultValue: '', description: '构建命令')

        // JaCoCo
        string(name: 'pathSource', defaultValue: '**/src/main/java', description: '')
        string(name: 'pathClass', defaultValue: '**/src/main/java', description: '')
        string(name: 'subDir', defaultValue: '**/src/main/java', description: 'Specify an alternative sub-directory as working directory for the task')

        // 邮件通知
        string(name: 'sendmail', defaultValue: 'yes', description: '是否发送邮件')
        string(name: 'projectRecipientList', defaultValue: '', description: '项目收件人列表（系统自动生成）')

    }
    agent {
        node {
            label 'java'
        }
    }
    stages {
        stage('检出项目') {
            steps {
                git branch: env.branch, credentialsId: 'admin', url: env.sshUrl
            }
        }
        stage('单元测试') {
            steps {
                script {
                    // 是否clean
                    if (params.isClean == 'true') {
                        sh "${params.mavenHome}/bin/mvn clean install"
                    }
                    // 进入子目录执行
                    if (params.subDir != '') {
                        sh "cd ${params.subDir}"
                    }
                    sh "${params.mavenHome}/bin/${params.build}"
                }
            }
        }
        stage('jacoco') {
            steps {
                jacoco classPattern: params.pathClass, sourcePattern: params.pathSource
                //jacoco classPattern: "**/seller-web/target/classes", sourcePattern: "**/seller-web/src/main/java"
            }
        }
    }
    post {
        always {
            step([$class: 'Publisher', reportFilenamePattern: '**/testng-results.xml'])
        }
        success {
            script {
                if (sendmail == 'yes') {
                    emailext body: '''
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="description" content="Clean Email Newsletter"/>
    <meta http-equiv="keywords" content="Newsletter, Email Template, Clean, Modern, cssMoban.com"/>
    <title>Infotech (Issue #1)</title>
    <style type="text/css">
        * {
            margin: auto;
            padding: 0;
        }
        li {
            padding-top: 2px;
            padding-bottom: 2px;
        }
        p {
            padding: 0px;
            margin: 0px;
        }

    </style>
</head>

<body style="text-align:center;margin:auto;padding:0;background: url(main_bg.gif) repeat;font-size:12px;font-family:Arial;color:#4a4a4a;">

<div style="background:#ffffff;width:600px;margin-top:10px;text-align:left;">
    
    <div style="background:#efefef;padding:10px;clear:both;margin-top:15px;height:112px;border-top:1px solid #dedede;border-bottom:1px solid #dedede;">
        <div style="float:left;width:380px;">
            <p style="font-size:20px;color:#ec8a1b;">
                <svg style="margin-top:6px;" width="25" fill="#FF4A00" height="25" role="img" viewBox="0 0 24 24"
                     xmlns="http://www.w3.org/2000/svg"><title>SoundCloud icon</title>
                    <path d="M1.175 12.225c-.051 0-.094.046-.101.1l-.233 2.154.233 2.105c.007.058.05.098.101.098.05 0 .09-.04.099-.098l.255-2.105-.27-2.154c0-.057-.045-.1-.09-.1m-.899.828c-.06 0-.091.037-.104.094L0 14.479l.165 1.308c0 .055.045.094.09.094s.089-.045.104-.104l.21-1.319-.21-1.334c0-.061-.044-.09-.09-.09m1.83-1.229c-.061 0-.12.045-.12.104l-.21 2.563.225 2.458c0 .06.045.12.119.12.061 0 .105-.061.121-.12l.254-2.474-.254-2.548c-.016-.06-.061-.12-.121-.12m.945-.089c-.075 0-.135.06-.15.135l-.193 2.64.21 2.544c.016.077.075.138.149.138.075 0 .135-.061.15-.15l.24-2.532-.24-2.623c0-.075-.06-.135-.135-.135l-.031-.017zm1.155.36c-.005-.09-.075-.149-.159-.149-.09 0-.158.06-.164.149l-.217 2.43.2 2.563c0 .09.075.157.159.157.074 0 .148-.068.148-.158l.227-2.563-.227-2.444.033.015zm.809-1.709c-.101 0-.18.09-.18.181l-.21 3.957.187 2.563c0 .09.08.164.18.164.094 0 .174-.09.18-.18l.209-2.563-.209-3.972c-.008-.104-.088-.18-.18-.18m.959-.914c-.105 0-.195.09-.203.194l-.18 4.872.165 2.548c0 .12.09.209.195.209.104 0 .194-.089.21-.209l.193-2.548-.192-4.856c-.016-.12-.105-.21-.21-.21m.989-.449c-.121 0-.211.089-.225.209l-.165 5.275.165 2.52c.014.119.104.225.225.225.119 0 .225-.105.225-.225l.195-2.52-.196-5.275c0-.12-.105-.225-.225-.225m1.245.045c0-.135-.105-.24-.24-.24-.119 0-.24.105-.24.24l-.149 5.441.149 2.503c.016.135.121.24.256.24s.24-.105.24-.24l.164-2.503-.164-5.456-.016.015zm.749-.134c-.135 0-.255.119-.255.254l-.15 5.322.15 2.473c0 .15.12.255.255.255s.255-.12.255-.27l.15-2.474-.165-5.307c0-.148-.12-.27-.271-.27m1.005.166c-.164 0-.284.135-.284.285l-.103 5.143.135 2.474c0 .149.119.277.284.277.149 0 .271-.12.284-.285l.121-2.443-.135-5.112c-.012-.164-.135-.285-.285-.285m1.184-.945c-.045-.029-.105-.044-.165-.044s-.119.015-.165.044c-.09.054-.149.15-.149.255v.061l-.104 6.048.115 2.449v.008c.008.06.03.135.074.18.058.061.142.104.234.104.08 0 .158-.044.209-.09.058-.06.091-.135.091-.225l.015-.24.117-2.203-.135-6.086c0-.104-.061-.193-.135-.239l-.002-.022zm1.006-.547c-.045-.045-.09-.061-.15-.061-.074 0-.149.016-.209.061-.075.061-.119.15-.119.24v.029l-.137 6.609.076 1.215.061 1.185c0 .164.148.314.328.314.181 0 .33-.15.33-.329l.15-2.414-.15-6.637c0-.12-.074-.221-.165-.277m8.934 3.777c-.405 0-.795.086-1.139.232-.24-2.654-2.46-4.736-5.188-4.736-.659 0-1.305.135-1.889.359-.225.09-.27.18-.285.359v9.368c.016.18.15.33.33.345h8.185C22.681 17.218 24 15.914 24 14.28s-1.319-2.952-2.938-2.952"/>
                </svg>
                <b>OPSCLOUD</b></p>
            <p style="font-size:15px;margin-top:10px;font-weight:bold;">Hi All</p>
            <p style="margin-top:6px;">${ENV, var="JOB_NAME"}持续集成测试报告.</p>
        </div>

        <div style="float:right;width:200px;">
            <p style="font-size:18px;color:#ec8a1b;">自动化测试报告</p>
            <ul style="font-size:8px;margin-top:5px;list-style-position:inside;">
                <li>项目名称 - ${PROJECT_NAME}</li>
                <li>构建结果 - <a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
                <li>项目连接 - <a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
                <li>构建连接 - <a href="${BUILD_URL}">${BUILD_URL}</a></li>
            </ul>
        </div>
    </div>

    <div style="margin:10px;clear:both;margin-top:15px;padding-bottom:10px;border-bottom:1px dashed #cccccc;">
        <p><span style="padding:2px 4px 2px 4px;font-size:13px;color:#ffffff;background:#666666;"><b>测试用例执行:</b></span>
        </p>

        <ul style="float:left;margin-top:5px;list-style-position:inside;">
            <li><font color="#0052CC">Total:${TEST_COUNTS,var="total"}</font></li>
            <li><font color="#006400">Pass: ${TEST_COUNTS,var="pass"}</font></li>
            <li><font color="#EF145F">Fail: ${TEST_COUNTS,var="fail"}</font></li>
            <li><font color="#FE7A16">Skip: ${TEST_COUNTS,var="skip"}</font></li>
        </ul>
        <p style="clear:both;margin-top:10px;"></p>
    </div>


    <div style="margin:10px;clear:both;margin-top:15px;padding-bottom:10px;border-bottom:1px dashed #cccccc;">
        <p><span style="padding:2px 4px 2px 4px;font-size:13px;color:#ffffff;background:#666666;"><b>自最后一次构建成功后的变化:</b></span>
        </p>
        <ul style="float:left;margin-top:5px;list-style-position:inside;">
            <li>在此查看历史变化记录: - <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
            <li>详细测试结果： <a href="${PROJECT_URL}lastCompletedBuild/testReport">${PROJECT_URL}lastCompletedBuild/testReport/</a>
            </li>
        </ul>
        <p style="clear:both;margin-top:10px;"></p>
    </div>
    
    <div style="margin:10px;clear:both;margin-top:15px;padding-bottom:10px;border-bottom:1px dashed #cccccc;">
        <p><span style="padding:2px 4px 2px 4px;font-size:13px;color:#ffffff;background:#666666;"><b>失败的测试结果:</b></span>
        </p>
        <ul style="float:left;margin-top:5px;list-style-position:inside;">
            <pre style="font-size:5pt; font-family:Tahoma, Arial, Helvetica, sans-serif">${FAILED_TESTS}</pre>
            <br/>
            <b><font color="#0B610B">构建日志 (最后 100 行):</font></b>
            <hr size="2" width="100%" align="center"/>
            <textarea cols="80" rows="30" readonly="readonly" style="font-family: Courier New">${BUILD_LOG, maxLines=100}</textarea>
        </ul>
        <p style="clear:both;margin-top:10px;"></p>
    </div>
    
</div>

<div style="width:600px;margin-top:10px;color:#999999;font-size:11px;">
    Structured Mail designed by <a href="#"><span style="color:#cccccc;text-decoration:underline;">https://github.com/ixrjog/opsCloud</span></a><br/>
    baiyi@gegejia.com
</div>

<br/>
</body>
</html>
''', subject: '${PROJECT_NAME}', to: '${params.projectRecipientList}'
                }
            }
        }
    }

}
